package com.woniuxy.carlife.controller;


import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.Result;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.StatusCode;
import com.woniuxy.carlife.domain.ServiceGood;
import com.woniuxy.carlife.param.ServiceGoodParam;
import com.woniuxy.carlife.param.ServiceGoodPicParam;
import com.woniuxy.carlife.param.ServiceGoodPriceParam;
import com.woniuxy.carlife.service.ServiceGoodService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author fx
 * @ClassName:ServiceGoodController
 * @Description: 服务商品控制类
 * @date 2021/4/6 20:00
 **/

@RestController
@RequestMapping("/serviceGood")
@Api(tags = "保养商品接口信息")
@Slf4j
public class ServiceGoodController {

    @Resource
    private ServiceGoodService service;

    /**
     * 功能描述〈添加保养商品〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/12 10:04
     * @throws
     */
    @ApiOperation(value = "添加保养商品",notes = "<span style='color:red;'>添加保养商品</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 20000,message = "添加保养商品成功"),
            @ApiResponse(code=20001,message = "添加保养商品失败")
    })
    @PostMapping("/merchantmanage/servicegood")
    public Result saveServiceGood(@RequestBody @ApiParam ServiceGoodParam serviceGood) throws IOException{
        log.info("saveServiceGood开始:参数：{}",serviceGood);
        //判断传入参数
        if (serviceGood.getServiceGoodBrand().trim().length()==0||
            serviceGood.getServiceGoodBrand()==null||
            serviceGood.getServiceGoodCategory().trim().length()==0||
            serviceGood.getServiceGoodCategory()==null||
            serviceGood.getServiceGoodName().trim().length()==0||
            serviceGood.getServiceGoodName()==null||
            serviceGood.getServiceGoodPic().trim().length()==0||
            serviceGood.getServiceGoodPic()==null||
            serviceGood.getServiceGoodPrice().toString().trim().length()==0||
            serviceGood.getServiceGoodPrice()==null)
        {
            log.info("saveServiceGood结束:返回：{}","传入参数错误");
            return new Result(false, StatusCode.PARAMERROR,"传入参数错误");
        }
        Integer i = service.saveServiceGood(serviceGood);
        if (i==1){
            log.info("saveServiceGood结束:返回：{}","添加服务商品成功");
            return new Result(true, StatusCode.OK,"添加服务商品成功");
        }else if (i==0){
            log.info("saveServiceGood结束:返回：{}","添加服务商品失败");
            return new Result(false, StatusCode.ERROR,"添加服务商品失败");
        }else{
            log.info("saveServiceGood结束:返回：{}","该商品已存在");
            return new Result(false, StatusCode.ERROR,"该商品已存在");
        }
    }

    /**
     * 功能描述〈根据门店id查询所有商品〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/12 10:19
     * @throws
     */
    @ApiOperation(value = "根据门店id查询所有商品",notes = "<span style='color:red;'>根据门店id查询所有商品</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 20000,message = "查询服务商品成功"),
            @ApiResponse(code=20001,message = "查询服务商品失败"),
            @ApiResponse(code=30001,message = "参数错误"),
            @ApiResponse(code=0,message = "没有数据")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId",value = "门店",dataType = "Integer",paramType = "query",example = "1"),
    })
    @GetMapping("/merchantmanage/servicegood")
    public Result findfindServiceGoodByStoreId(Integer storeId){
        log.info("findfindServiceGoodByStoreId开始:参数：{}",storeId);
        //判断传入参数
        if (storeId==null||storeId<=0){
            log.info("findfindServiceGoodByStoreId结束:返回：{}","传入参数的门店id为空或者小于1");
            return new Result(false, StatusCode.PARAMERROR,"传入参数错误");
        }
        List<ServiceGood> goods = service.findfindServiceGoodByStoreId(storeId);
        //判断结果
        if (ObjectUtils.isEmpty(goods)){
            log.info("findfindServiceGoodByStoreId结束:返回：{}","查询失败，没找到数据");
            return new Result(false, StatusCode.NULLDATA,"查询失败，没找到数据");
        }
        log.info("findfindServiceGoodByStoreId结束:返回：{}",goods);
        return new Result(true, StatusCode.OK,"查询服务商品成功",goods);
    }

    /**
     * 功能描述〈根据门店id查询所有上架商品〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/12 10:20
     * @throws
     */
    @ApiOperation(value = "根据门店id查询所有上架商品",notes = "<span style='color:red;'>根据门店id查询所有上架商品</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 20000,message = "查询服务商品成功",response = ServiceGood.class),
            @ApiResponse(code=20001,message = "查询服务商品失败"),
            @ApiResponse(code=30001,message = "参数错误"),
            @ApiResponse(code=0,message = "没有数据")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId",value = "门店",dataType = "Integer",paramType = "query",example = "1"),
    })
    @GetMapping("servicegoodallu")
    public Result findServiceGoodByStatusAndStoreId(Integer storeId){
        log.info("findServiceGoodByStatusAndStoreId开始:参数：{}",storeId);
        //判断传入参数
        if (storeId==null||storeId<=0){
            log.info("findServiceGoodByStatusAndStoreId结束:返回：{}","传入参数的门店id为空或者小于1");
            return new Result(false, StatusCode.PARAMERROR,"传入参数错误");
        }
        List<ServiceGood> serviceGoodByStatusAndStoreId =
                service.findServiceGoodByStatusAndStoreId(storeId);
        if (ObjectUtils.isEmpty(serviceGoodByStatusAndStoreId)){
            log.info("findServiceGoodByStatusAndStoreId结束:返回：{}","查询失败，没有查询到数据");
            return new Result(false, StatusCode.NULLDATA,"查询失败，没有查询到数据");
        }
        log.info("findServiceGoodByStatusAndStoreId结束:返回：{}",serviceGoodByStatusAndStoreId);
        return new Result(true, StatusCode.OK,"查询服务商品成功",serviceGoodByStatusAndStoreId);
    }

    /**
     * 功能描述〈根据商品id修改状态为下架〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/12 10:50
     * @throws
     */
    @ApiOperation(value = "根据商品id修改状态为下架",notes = "<span style='color:red;'>根据商品id修改状态为下架</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 20000,message = "下架商品成功"),
            @ApiResponse(code=20001,message = "下架商品失败"),
            @ApiResponse(code=30001,message = "参数错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodId",value = "商品id",dataType = "Integer",paramType = "query",example = "1"),
    })
    @PutMapping("/merchantmanage/servicegoodalld")
    public Result updateServiceGoodStatusToDown(Integer goodId){
        log.info("updateServiceGoodStatusToDown开始:参数：{}",goodId);
        //判断传入参数
        if (goodId==null||goodId<=0){
            log.info("updateServiceGoodStatusToDown结束:返回：{}","传入参数的商品id为空或者小于1");
            return new Result(false, StatusCode.PARAMERROR,"传入参数错误");
        }
        boolean b = service.updateServiceGoodStatusToDown(goodId);
        if (b){
            log.info("updateServiceGoodStatusToDown结束:返回：{}","下架商品成功");
            return new Result(true, StatusCode.OK,"下架商品成功");
        }
        log.info("updateServiceGoodStatusToDown结束:返回：{}","下架商品失败");
        return new Result(false, StatusCode.ERROR,"下架商品失败");
    }

    /**
     * 功能描述〈根据商品id修改商品价格〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/12 10:54
     * @throws
     */
    @ApiOperation(value = "根据商品id修改商品价格",notes = "<span style='color:red;'>根据商品id修改商品价格</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 20000,message = "修改商品价格成功"),
            @ApiResponse(code=20001,message = "修改商品价格失败"),
            @ApiResponse(code=30001,message = "参数错误")
    })
    @PutMapping("/merchantmanage/servicegoodprice")
    public Result updateServiceGoodPriceByGoodId(@RequestBody @ApiParam ServiceGoodPriceParam goodParam){
        log.info("updateServiceGoodPriceByGoodId开始:参数：{}",goodParam);
        //判断参数错误
        if (goodParam.getGoodId()==null||
            goodParam.getGoodId().toString().trim().length()==0||
            goodParam.getPrice()==null||
            //前面的与后面比较，等于-->0  大于-->1  小于-->-1
            goodParam.getPrice().compareTo(new BigDecimal(0))==-1)
        {
            log.info("updateServiceGoodPriceByGoodId结束:返回：{}","参数错误");
            return new Result(false, StatusCode.PARAMERROR,"输入参数错误");
        }
        boolean b = service.updateServiceGoodPriceByGoodId(goodParam);
        if (b){
            log.info("updateServiceGoodPriceByGoodId结束:返回：{}","修改商品价格成功");
            return new Result(true, StatusCode.OK,"修改商品价格成功");
        }
        log.info("updateServiceGoodPriceByGoodId结束:返回：{}","修改商品价格失败");
        return new Result(false, StatusCode.ERROR,"修改商品价格失败");
    }

    /**
     * 功能描述〈上传商品图片〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/13 3:40
     * @throws
     */
    @ApiOperation(value = "上传商品图片",notes = "<span style='color:red;'>上传商品图片，单张图片大小限制为1m</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 20000,message = "上传图片成功"),
            @ApiResponse(code=20001,message = "上传图片失败")
    })
    @PostMapping("/merchantmanage/goodpic")
    public Result saveServiceGoodPic(@ModelAttribute @ApiParam MultipartFile pic) throws IOException {
        log.info("saveServiceGoodPic开始:参数：{}",pic);
        String s = service.saveServiceGoodPic(pic);
        System.out.println(s);
        log.info("saveServiceGoodPic结束:返回：{}",s);
        return new Result(true,StatusCode.OK,"上传图片成功",s);
    }

    /**
     * 功能描述〈根据商品id修改图片〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/14 15:50
     * @throws
     */
    @ApiOperation(value = "根据商品id修改图片",notes = "<span style='color:red;'>根据商品id修改图片</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 20000,message = "修改图片成功"),
            @ApiResponse(code=20001,message = "修改图片成功失败"),
            @ApiResponse(code=30001,message = "参数错误")
    })
    @PutMapping("/merchantmanage/servicegoodpic")
    public Result updateServiceGoodPic(@RequestBody @ApiParam ServiceGoodPicParam goodPicParam){
        log.info("updateServiceGoodPic开始：参数:{}",goodPicParam);
        if (goodPicParam.getGoodId()==null||
                goodPicParam.getGoodId().toString().trim().length()==0||
                goodPicParam.getPic()==null||
                goodPicParam.getPic().trim().length()==0)
        {
            log.info("updateServiceGoodPic结束:返回：{}","参数错误");
            return new Result(false, StatusCode.PARAMERROR,"输入参数错误");
        }
        boolean b = service.updateServiceGoodPicByGoodId(goodPicParam);
        if (b){
            log.info("updateServiceGoodPic结束:返回：{}","修改图片成功");
            return new Result(true, StatusCode.OK,"修改图片成功");
        }
        log.info("updateServiceGoodPic结束:返回：{}","修改图片失败");
        return new Result(false, StatusCode.ERROR,"修改图片失败");
    }
}

